package com.cn.yang.service.impl;

import com.cn.yang.dao.ProductDao;
import com.cn.yang.dao.impl.ProductDaoImpl;
import com.cn.yang.model.production.ProductClassifyModel;
import com.cn.yang.model.production.ProductModel;
import com.cn.yang.service.ProductService;
import com.cn.yang.util.PageUtil;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    //查询产品种类===========
    @Override
    public List<ProductClassifyModel> findClassify() {
        ProductDao productDao = new ProductDaoImpl();
        Jedis jedis = new Jedis();
        long length = jedis.llen("productClassify_id");
        List<ProductClassifyModel> list = new ArrayList<>();
        if (length>0){
            List<String> productClassify_id = jedis.lrange("productClassify_id",0,-1);
            List<String> productClassify_name = jedis.lrange("productClassify_name",0,-1);
            for (int i =0;i<length;i++){
                ProductClassifyModel model = new ProductClassifyModel();
                model.setId(Integer.parseInt(productClassify_id.get(i)));
                model.setName(productClassify_name.get(i));
                list.add(model);
            }
        }else {
            list = productDao.findClassify();
            for (int i = 0;i < list.size();i++){
                jedis.rpush("productClassify_id", String.valueOf(list.get(i).getId()));
                jedis.rpush("productClassify_name",list.get(i).getName());
            }
        }
        return list;
    }
//添加产品信息========
    @Override
    public int addProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.addProduct(model);
    }
//查找 产品信息=========
    @Override
    public List<ProductModel> findProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.findProduct(model);
    }
    public List<ProductModel> findProduct() {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.findProduct();
    }
//产品总数
    @Override
    public int countProduct() {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.countProduct();
    }
//删除某个产品
    @Override
    public int deleteById(Integer id) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.deleteById(id);
    }

    @Override
    public ProductModel selectById(Integer id) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.selectById(id);
    }

    @Override
    public int updateProduct(ProductModel model) {
        ProductDao productDao = new ProductDaoImpl();
        return productDao.updateProduct(model);
    }
}
